package com.ljx.config;

import com.ljx.filter.MyFilter;
import com.ljx.servlet.MyServlet;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.Filter;
import javax.servlet.Servlet;
import javax.servlet.http.HttpServlet;

/**
 * @author LJX
 * @Description: TODO
 * @Date: 2022/11/16 9:39
 * @Version 1.0
 */
/**
 * 表示该类为配置类，等价于我们之前的xml文件
 * @author 22732
 */
@Configuration
public class MyConfig {
	/**
	 * 注册servlet组件
	 * @Bean 类似于xml文件中 <bean class=""/> 把方法返回的对象交于spring容器管理
	 * @return
	 */
	@Bean
	public ServletRegistrationBean<HttpServlet> registrationBean(){
		ServletRegistrationBean<HttpServlet> registrationBean = new ServletRegistrationBean<>();
		// 类似于  <servlet-name>springmvc</servlet-name>
		registrationBean.setName("my");
		// <url-pattern>/</url-pattern>
		registrationBean.addUrlMappings("/my");
		//  <servlet-class>
		registrationBean.setServlet(new MyServlet());
		return registrationBean;

	}
	/**
	 * 注册filter组件
	 */

	@Bean
	public FilterRegistrationBean filterRegistrationBean(){
		FilterRegistrationBean<Filter> filterFilterRegistrationBean = new FilterRegistrationBean<>();
		filterFilterRegistrationBean.setName("filter");
		filterFilterRegistrationBean.addUrlPatterns("/*");
		filterFilterRegistrationBean.setFilter(new MyFilter());
		return filterFilterRegistrationBean;
	}

}
